﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace TitleText
{
    public class Realization
    {
        public static int rightNumber { get; set; }//记录全部答对的
        public static int wrongnumber { get; set; }//记录全部答错的

        //这里是显示题目的文本
        const string QuestionAnswer = @"C:\Users\Administrator.SC-202003060016\Desktop\WPF\FishWarehouse\TitleText\QuestionAnswer.txt";
        //这里是显示答案的文本
        const string Question = @"C:\Users\Administrator.SC-202003060016\Desktop\WPF\FishWarehouse\TitleText\Question.txt";
        //const string daan = @"C:\Users\武\Desktop\MyCAIDemo\TitleText\dana.txt";
        public static string GetRandom(int count, int Range1, int Range2, int operators, out int value)
        {
            char[] op = { '+', '-', '*', '/' };
            Random ran = new Random();
            string t = "";
            value = 0;
            for (int i = 0; i < count; i++)
            {
                int num1 = ran.Next(Range1, Range2);
                int num2 = ran.Next(Range1, Range2);
                var ranoperators = op[ran.Next(operators)];
                if (operators >= 1 && operators <= 4)
                {
                    if (ranoperators == '+')
                    {
                        t = num1 + op[0].ToString() + num2 + "="; value = (num1 + num2);
                    }
                    else if (ranoperators == '-')
                    {
                        if (num1 > num2)
                        {
                            t = num1 + op[1].ToString() + num2 + "="; value = (num1 - num2);
                        }
                        else
                        {
                            t = num2 + op[1].ToString() + num1 + "="; value = (num2 - num1);
                        }
                    }
                    else if (ranoperators == '*')
                    {
                        t = num1 + op[2].ToString() + num2 + "="; value = (num1 * num2);
                    }
                    else
                    {
                        if (num1 > num2)
                        {
                            t = num1 + op[3].ToString() + num2 + "="; value = (num1 / num2);
                        }
                        else
                        {
                            t = num2 + op[3].ToString() + num1 + "="; value = (num2 / num1);

                        }
                    }
                }
                else
                {
                   return "输入错误";
                }
            }
            return t;
        }
        public static void CreateSampleFileAsync(int count, int Range, int Range2, int operators)
        {
            FileStream streams = File.Create(Question);
            var writers = new StreamWriter(streams);
            using (FileStream stream = File.Create(QuestionAnswer))
            {
                using (var writer = new StreamWriter(stream))
                {

                    int[] vs = new int[count];
                    for (int i = 1; i < vs.Length + 1; i++)
                    {
                        string s = $"{i}#-------题目：{GetRandom(count, Range, Range2, operators, out int value)}{Environment.NewLine}";
                        writer.WriteAsync(s);

                        string ss = $"{value}{Environment.NewLine}";
                        writers.WriteAsync(ss);

                    }
                    writers.Dispose();
                    streams.Dispose();
                }
            }
        }

        //public static string Read(string s)
        //{
        //    string[] QuestionAnswers = System.IO.File.ReadAllLines(QuestionAnswer);
        //    string[] Questions = System.IO.File.ReadAllLines(Question);
        //    string ss;
        //    FileStream streams = File.Create(daan);
        //    var writers = new StreamWriter(streams);
        //    for (int i = 0; i < QuestionAnswers.Length; i++)
        //    {
        //        //Console.WriteLine(QuestionAnswers[i]);        
        //        for (int j = i; j < Questions.Length; j++)
        //        {
        //            if (Questions[j].ToString() == s)
        //            {
        //                rightNumber++;
        //                ss = $"题目：{QuestionAnswers[i]}# 输入的答案：{s}  ×{Environment.NewLine}";
        //                writers.WriteAsync(ss);
                        
        //            }
        //            else
        //            {
        //                wrongnumber++;
        //                ss = $"题目：{QuestionAnswers[i]}# 输入的答案：{s}  √{Environment.NewLine}";
        //                writers.WriteAsync(ss);
                        
        //            }

        //        }
        //        return QuestionAnswers[i];
        //    }
        //    writers.Dispose();
        //    return null;
        //}

        public static List<string> GetQuestionAnswer()//浏览题目记录
        {
            using (System.IO.StreamReader sr = new System.IO.StreamReader(QuestionAnswer))
            {
                string topic;
                List<string> vs=new List<string>();
                while ((topic = sr.ReadLine()) != null)
                {
                    vs.Add(topic);
                   
                }
                return vs;
            }
        }
        public static List<string> GetQuestion()//浏览答案记录
        {
            using (System.IO.StreamReader sr = new System.IO.StreamReader(Question))
            {
                string topic;
                List<string> vs = new List<string>();
                while ((topic = sr.ReadLine()) != null)
                {
                    vs.Add(topic);

                }
                return vs;
            }
        }
        //public static string results()//统计结果！
        //{
        //    return $"总共做了{rightNumber + wrongnumber}道题：做对了{rightNumber}道题,做错了{wrongnumber}道题。";
        //}

    }
}
